<template>
	<view class="normal-login-container">
		<view class="logo-content align-center justify-center flex">
			<image style="width: 400rpx;height: 400rpx;" src="@/static/images/logo.png" mode="widthFix">
			</image>
		</view>
		<view class="login-form-content">
			<view class="action-btn">
				<button @click="weixinLogin" class="login-btn cu-btn block bg-blue lg round">微信一键登录</button>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		bindingWeixin,
		loginWeixin
	} from "@/api/login.js"

	export default {
		name: 'Login',
		data() {
			return {
				captchaEnabled: false, // 验证码开关 TODO 芋艿：需要抽到配置里
				globalConfig: getApp().globalData.config,
				loginForm: {
					username: "",
					password: "",
					captchaVerification: ""
				}
			}
		},
		methods: {
			//微信一键登录
			weixinLogin() {
				let that = this
				that.$modal.loading("登录中")
				uni.login({
					provider: 'weixin', //使用微信登录
					success: function(loginRes) {
						let datas = {
							type: 34,
							code: loginRes.code,
							state: 'default',
						}
						loginWeixin(datas).then(response => {
							that.$modal.closeLoading()
							// console.log('绑微信',response)
							that.$store.dispatch('WeixinLogin', response.data).then(() => {
								that.loginSuccess()
							})
						})
					}
				});
			},
			// 密码登录
			async pwdLogin(captchaParams) {
				this.$modal.loading("登录中，请耐心等待...")
				// 执行登录
				this.loginForm.captchaVerification = captchaParams.captchaVerification
				this.$store.dispatch('Login', this.loginForm).then(() => {
					this.$modal.closeLoading()
					this.loginSuccess()
				})
			},
			// // 登录成功后，处理函数
			loginSuccess(result) {
			// 	// 设置用户信息/现只有微信登录
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/index')
			// 		// this.$tab.navigateBack(1)
			// 		// bindingWeixin().then(response => {
			// 		// 	// console.log('是否绑定', response.socialUsers)
			// 		// 	let floge = this.handleData(response.data.socialUsers)
			// 		// 	if(floge){
			// 		// 		this.$tab.reLaunch('/pagesA/work/index')
			// 		// 	}else{
			// 		// 		this.$tab.reLaunch('/pagesA/authorize/authorize')
			// 		// 	}
			// 		// })
				})
			},
			//处理数据并存入缓存
			handleData(array) {
				const itemWithType = array.find(item => item.type === 34);
				if (itemWithType) {
					uni.setStorageSync('openid', itemWithType.openid);
					return true;
				}else{
					return false;
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
		width: 100%;
		margin-top: 300rpx;
		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			// padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			// margin-top: 2%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 20px;
				height: 45px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}
		}

		.easyinput {
			width: 100%;
		}
	}

	.login-code-img {
		height: 45px;
	}
</style>